
import {getsdecimal} from '@/api/api'
  export default {
      data(){
          return{
            websocketData:[],
            websocketDataps:{}
          }
      },
      methods:{
        // 科学计数法转化数组
        c(param) {
            let strParam = String(param)
            let flag = /e/.test(strParam)
            if (!flag) return param
            // 指数符号 true: 正，false: 负
            let sysbol = true
            if (/e-/.test(strParam)) {
              sysbol = false
            }
            // 指数
            let index = Number(strParam.match(/\d+$/)[0])
            // 基数
            let basis = strParam.match(/^[\d\.]+/)[0].replace(/\./, '')
            if (sysbol) {
              return basis.padEnd(index + 1, 0)
            } else {
              return basis.padStart(index + basis.length, 0).replace(/^0/, '0.')
            }
      },
        getsdecimallength(){
          let decimalparams={
                platform:'real',
                needThreeMoth:'false'
          } 
    getsdecimal(decimalparams,'/market/v2/product').then(res=>{
        if(res.status == 200){
        let data = res.data
        let id = 0
            for(let i = 0;i<data.length;i++){
                for(let j = 0;j<data[i]['list'].length;j++){
                    id++
                let p_item = data[i].list[j];
                let item = {id :id, s2: 0, b1: 0 ,decimal:0};
                    data[i]['list'][j]['id'] = id
                    this.websocketData.push(data[i]['list'][j])
                let PriceChanges = data[i]['list'][j]['minPriceChanges']

                     if (PriceChanges) {
                        PriceChanges = this.c(PriceChanges)
                        PriceChanges = PriceChanges.toString();
                        PriceChanges = PriceChanges.split(".")[1];
                        PriceChanges = PriceChanges.length
                        } else {
                              PriceChanges = 2;
                      }

                    item.decimal= PriceChanges
                    let name = p_item.feed_code
                    this.$set( this.websocketDataps,name,item)
                } 
            }
                this.WebSocketTest()
        }   
        })
        },
    WebSocketTest() {
    let _this = this

      var ws;
      if ("WebSocket" in window) {
        ws = new WebSocket("wss://mihuiapi.com/cuws");
        ws.onopen = function () {
        ws.send(JSON.stringify({op: 'auth', args: 'hkcuqwert!@#$'}));
        };
        ws.onmessage = function (evt) {
          let received_msg = JSON.parse(evt.data);
            for(let i = 0;i<received_msg.length;i++){
                if(received_msg[i]['op'] == 'orderBookL1'){
                    let name = received_msg[i]['data']['p']
                    let b1 = received_msg[i]['data']['b1']
                    let s2 = received_msg[i]['data']['s2']
                    if(_this.websocketDataps[name]){
                    let decimal = _this.websocketDataps[name]['decimal']
                    _this.$set( _this.websocketDataps[name],'b1',b1.toFixed(decimal))
                    _this.$set( _this.websocketDataps[name],'s2',s2.toFixed(decimal))
                    }
                }
            }
                  _this.$forceUpdate();
        };
        ws.onclose = function () {};
      } else {
        alert("您的浏览器不支持 WebSocket!");
      }
    },
   
      },
      mounted(){
        

      }
  }